package com.internetCafes.spms.web.customer.service.impl.msg;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.internetCafes.spms.web.customer.common.cont.CommonConst;
import com.internetCafes.spms.web.customer.entity.msg.Msg;
import com.internetCafes.spms.web.customer.entity.msg.MsgUserAsso;
import com.internetCafes.spms.web.customer.mapper.msg.MsgMapper;
import com.internetCafes.spms.web.customer.model.msg.MsgListDo;
import com.internetCafes.spms.web.customer.model.msg.MsgListPo;
import com.internetCafes.spms.web.customer.model.msg.MsgPagingDo;
import com.internetCafes.spms.web.customer.model.msg.MsgPagingPo;
import com.internetCafes.spms.web.customer.service.msg.IMsgService;
import com.internetCafes.spms.web.customer.service.msg.IMsgUserAssoService;
import com.sms.common.util.hutool.core.collection.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 平台消息表 服务实现类
 * </p>
 *
 * @author Zj
 * @since 2021-10-25
 */
@Service
@Slf4j
public class MsgServiceImpl extends ServiceImpl<MsgMapper, Msg> implements IMsgService {

    @Autowired
    private IMsgUserAssoService userAssoService;

    @Override
    public void saveBatchMsgNoSqlParser(List<Msg> msgList) {
        // 保存信息
        boolean saveResult = baseMapper.saveBatchNoSqlParser(msgList);

        if (!saveResult) {
            log.error("消息保存失败！");
            return;
        }

        List<MsgUserAsso> userAssoBatch = msgList.stream()
                .filter(msg -> CollectionUtil.isNotEmpty(msg.getReceiverIdList()))
                .map(msg ->
                        msg.getReceiverIdList()
                                .stream()
                                .distinct()
                                .map(receiverId ->
                                        new MsgUserAsso()
                                                .setTenantId(msg.getTenantId())
                                                .setCreateTime(msg.getCreateTime())
                                                .setMsgId(msg.getId())
                                                .setReceiverId(receiverId)
                                                .setReadFlag(CommonConst.FlagEnum.NOT.getId())
                                                .setCreateByName(msg.getCreateByName())
                                                .setCreateBy(msg.getCreateBy()))
                                .collect(Collectors.toList()))
                .flatMap(Collection::stream)
                .distinct()
                .collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(userAssoBatch)) {
            userAssoService.saveBatchNoSqlParser(userAssoBatch);
        }
    }

    @Override
    public void saveBatchMsg(List<Msg> msgList) {
        // 保存信息
        boolean saveResult = saveBatch(msgList);

        if (!saveResult) {
            log.error("消息保存失败！");
            return;
        }

        List<MsgUserAsso> userAssoBatch = msgList.stream()
                .filter(msg -> CollectionUtil.isNotEmpty(msg.getReceiverIdList()))
                .map(msg ->
                        msg.getReceiverIdList()
                                .stream()
                                .distinct()
                                .map(receiverId ->
                                        new MsgUserAsso()
                                                .setMsgId(msg.getId())
                                                .setReceiverId(receiverId)
                                                .setReadFlag(CommonConst.FlagEnum.NOT.getId())
                                                .setCreateByName(msg.getCreateByName())
                                                .setCreateBy(msg.getCreateBy()))
                                .collect(Collectors.toList()))
                .flatMap(Collection::stream)
                .distinct()
                .collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(userAssoBatch)) {
            userAssoService.saveBatch(userAssoBatch);
        }
    }

    @Override
    public void saveMsg(Msg msg) {
        // 保存信息
        boolean saveResult = save(msg);

        if (!saveResult) {
            log.error("消息保存失败！");
            return;
        }
        // 批量保存信息接收人信息
        List<MsgUserAsso> userAssoBatch = msg.getReceiverIdList()
                .stream()
                .distinct()
                .map(receiverId ->
                        new MsgUserAsso()
                                .setMsgId(msg.getId())
                                .setReceiverId(receiverId)
                                .setReadFlag(CommonConst.FlagEnum.NOT.getId())
                                .setCreateByName(msg.getCreateByName())
                                .setCreateBy(msg.getCreateBy()))
                .collect(Collectors.toList());
        if (CollectionUtil.isNotEmpty(userAssoBatch)) {
            userAssoService.saveBatch(userAssoBatch);
        }
    }

    @Override
    public Page<MsgPagingPo> poPaging(Page<Msg> page, MsgPagingDo pagingDo) {
        return baseMapper.poPaging(page, pagingDo);
    }

    @Override
    public List<MsgListPo> list(MsgListDo listDo) {
        return baseMapper.list(listDo);
    }
}
